package com.imut.lagain.service.impl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imut.lagain.entity.HumorMessage;
import com.imut.lagain.mapper.HumorMessageMapper;
import com.imut.lagain.repository.HumorMessageRepository;
import com.imut.lagain.service.IHumorMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

/**
 * 幽默文案服务实现类
 */
@Service
public class HumorMessageServiceImpl extends ServiceImpl<HumorMessageMapper, HumorMessage> implements IHumorMessageService {
    private static final Logger log = LoggerFactory.getLogger(HumorMessageServiceImpl.class);

    
    @Autowired
    private HumorMessageRepository humorMessageRepository;
    
    /**
     * 根据心情类型和小偷类型查询幽默文案
     * @param moodType 心情类型
     * @param thiefType 小偷类型
     * @return 幽默文案列表
     */
    @Override
    public List<HumorMessage> findByMoodTypeAndThiefType(String moodType, String thiefType) {
        return humorMessageRepository.findByMoodTypeAndThiefType(moodType, thiefType);
    }
    
    /**
     * 根据心情类型查询幽默文案
     * @param moodType 心情类型
     * @param page 分页参数
     * @return 幽默文案分页列表
     */
    @Override
    public IPage<HumorMessage> findByMoodType(String moodType, Page<HumorMessage> page) {
        return humorMessageRepository.findByMoodType(moodType, page);
    }
    
    /**
     * 根据小偷类型查询幽默文案
     * @param thiefType 小偷类型
     * @param page 分页参数
     * @return 幽默文案分页列表
     */
    @Override
    public IPage<HumorMessage> findByThiefType(String thiefType, Page<HumorMessage> page) {
        return humorMessageRepository.findByThiefType(thiefType, page);
    }
    
    /**
     * 查询所有未删除的幽默文案
     * @return 幽默文案列表
     */
    @Override
    public List<HumorMessage> findAllActive() {
        return humorMessageRepository.findAllActive();
    }
    
    /**
     * 根据文案内容模糊查询幽默文案
     * @param message 文案内容关键字
     * @param page 分页参数
     * @return 幽默文案分页列表
     */
    @Override
    public IPage<HumorMessage> findByMessageLike(String message, Page<HumorMessage> page) {
        return humorMessageRepository.findByMessageLike(message, page);
    }
    
    /**
     * 获取随机幽默文案
     * @param moodType 心情类型
     * @param thiefType 小偷类型
     * @return 随机幽默文案
     */
    @Override
    public HumorMessage getRandomHumorMessage(String moodType, String thiefType) {
        try {
            List<HumorMessage> messages;
            if (moodType != null && thiefType != null) {
                messages = humorMessageRepository.findByMoodTypeAndThiefType(moodType, thiefType);
            } else if (moodType != null) {
                messages = humorMessageRepository.findByMoodType(moodType, new Page<>(1, 1000)).getRecords();
            } else if (thiefType != null) {
                messages = humorMessageRepository.findByThiefType(thiefType, new Page<>(1, 1000)).getRecords();
            } else {
                messages = humorMessageRepository.findAllActive();
            }
            
            if (messages.isEmpty()) {
                return null;
            }
            
            java.util.Random random = new java.util.Random();
            return messages.get(random.nextInt(messages.size()));
        } catch (Exception e) {
            log.error("获取随机幽默文案失败", e);
            return null;
        }
    }
    
    /**
     * 获取幽默文案列表
     * @param page 分页参数
     * @param moodType 心情类型
     * @param thiefType 小偷类型
     * @return 幽默文案分页列表
     */
    @Override
    public IPage<HumorMessage> getHumorMessageList(Page<HumorMessage> page, String moodType, String thiefType) {
        try {
            if (moodType != null && thiefType != null) {
                // 需要在Repository中添加支持分页的方法
                List<HumorMessage> messages = humorMessageRepository.findByMoodTypeAndThiefType(moodType, thiefType);
                // 手动分页
                int start = (int) ((page.getCurrent() - 1) * page.getSize());
                int end = Math.min(start + (int) page.getSize(), messages.size());
                List<HumorMessage> pageMessages = messages.subList(start, end);
                
                IPage<HumorMessage> result = new Page<>(page.getCurrent(), page.getSize());
                result.setRecords(pageMessages);
                result.setTotal(messages.size());
                return result;
            } else if (moodType != null) {
                return humorMessageRepository.findByMoodType(moodType, page);
            } else if (thiefType != null) {
                return humorMessageRepository.findByThiefType(thiefType, page);
            } else {
                // 查询所有活跃的幽默文案
                return humorMessageRepository.selectPage(page, null);
            }
        } catch (Exception e) {
            log.error("获取幽默文案列表失败", e);
            return new Page<>();
        }
    }
}
